from django.contrib import admin
from django import forms


from django.contrib import messages
from interview.models import Candidate
from datetime import datetime

from jobs.models import Job
from jobs.models import Resume


class JobAdmin(admin.ModelAdmin):
    exclude = ('creator','created_date','modified_date')
    list_display = ('job_name', 'job_type', 'job_city', 'creator', 'created_date', 'modified_date')

    def save_model(self, request, obj, form, change):
        if obj.creator is None:
            obj.creator = request.user
        super().save_model(request, obj, form, change)


def enter_interview_process(modeladmin, request, queryset):
    candidate_names = ""

    for resume in queryset:
        candidate = Candidate()
        # 把 obj 对象中的所有属性拷贝到 candidate 对象中:
        candidate.__dict__.update(resume.__dict__)
        candidate.created_date = datetime.now()
        candidate.modified_date = datetime.now()
        candidate_names = candidate.username + "," + candidate_names
        candidate.creator = request.user.username
        candidate.resume = resume
        candidate.save()
    messages.add_message(request, messages.INFO, '候选人: %s 已成功进入面试流程' % (candidate_names) )


enter_interview_process.short_description = u"进入面试流程"

from django.utils.html import format_html


class ResumeAdmin(admin.ModelAdmin):
    # 每页显示7条
    list_per_page = 7

    actions = (enter_interview_process,)


    def get_queryset(self, request):

        queryset = super().get_queryset(request)
        if not request.user.is_superuser:
            queryset = queryset.filter(uploader_id=request.user.id)
        return queryset

    def image_tag(self, obj):              
        if obj.picture:
            return format_html('<img src="{}" style="width:100px;height:80px;"/>'.format(obj.picture.url))
        return ""
    image_tag.allow_tags = True
    image_tag.short_description = 'Image'

    def get_candidate_introduction(self, obj):
        return obj.candidate_introduction[:50]

    get_candidate_introduction.short_description = '自我介绍'


    def get_work_expirence(self, obj):
        return obj.work_experience[:50]

    get_work_expirence.short_description = '工作经验'

    def get_company_name(self, obj):
        if len(obj.companies.all()) > 0:
            return obj.companies.first().name

    get_company_name.short_description = '公司名称'

    def get_start_time(self, obj):
        if len(obj.companies.all()) > 0:
            return obj.companies.first().start_time

    get_start_time.short_description = '开始时间'

    def get_end_time(self, obj):
        if len(obj.companies.all()) > 0:
            return obj.companies.first().end_time

    get_end_time.short_description = '结束时间'

    def get_project_experience(self, obj):
        return obj.project_experience[:50]

    get_project_experience.short_description = '项目经历'

    # list_display = ('username', 'applicant', 'city', 'apply_position', 'bachelor_school', 'master_school', 'image_tag', 'major','created_date')
    list_display = ('username', 'age', 'gender', 'city', 'apply_position', 'bachelor_school', 'major','get_candidate_introduction',
                    'get_work_expirence','get_company_name', 'get_start_time', 'get_end_time', 'get_project_experience', 'created_date')

    readonly_fields = ('applicant', 'created_date', 'modified_date',)

    fieldsets = (
        (None, {'fields': (
            "applicant", ("username", "city", "phone"),
            ("email", "apply_position", "born_address", "gender", ), ("picture", "attachment",),
            ("bachelor_school", "master_school"), ("major", "degree"), ('created_date', 'modified_date'),
            "candidate_introduction", "work_experience","project_experience",)}),
    )

    def save_model(self, request, obj, form, change):
        obj.applicant = request.user
        super().save_model(request, obj, form, change)


# Register your models here.
admin.site.register(Job, JobAdmin)
admin.site.register(Resume, ResumeAdmin)